﻿using System;
using System.Text;
using System.Security.Cryptography;

namespace Common.Security
{
    public sealed class Hash
    {
        #region 哈希Sha1加密
        /// <summary>
        /// 哈希Sha1加密
        /// </summary>
        /// <param name="strSrc">源字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string Sha1(string strSrc)
        {
            SHA1 hs = new SHA1CryptoServiceProvider();
            byte[] byteSrc = UTF8Encoding.Default.GetBytes(strSrc);
            byte[] byteAim = hs.ComputeHash(byteSrc);
            return BitConverter.ToString(byteAim);
        }
        #endregion
        #region 哈希Md5加密
        /// <summary>
        /// 哈希Md5加密
        /// </summary>
        /// <param name="input">未加密的密码</param>
        /// <returns>已加密的密码</returns>
        public static string Md5(string strSrc)
        {
            MD5 md5 = MD5.Create();
            byte[] byteSrc = md5.ComputeHash(Encoding.Default.GetBytes(strSrc));
            StringBuilder strAim = new StringBuilder();
            for (int i = 0; i < byteSrc.Length; i++)
            {
                strAim.Append(byteSrc[i].ToString("x2"));
            }
            return strAim.ToString();
        }
        #endregion
    }
}
